"use strict";

var path = require("path");
var gulp = require("gulp");
var notify = require("node-notifier");
var gload = require("gulp-load-plugins");
var gtasks = require('gulp-task-loader');

/**
 * Register gulp plugins
 *
 * @var {object} plugins
 * @var {boolean} plugins.debug
 * @var {string[]} plugins.pattern - glob to search for
 * @var {string[]} plugins.scope - dependencies from package.json
 * @var {boolean} plugins.lazy - lazy load the plugin
 */
var plugins = gload({
  debug: true,
  pattern: ["gulp-*"],
  scope: ["devDependencies"],
  lazy: false,
  rename: {
    "gulp-markdown-docs": "markdownDoc",
    "gulp-react-docs": "reactDoc"
  }
});

/*
 * Register gulp tasks
 */
gtasks({plugins: plugins, dir: "tasks"});

/*
 * Define tasks
 */
/**
 * Minifying need the sass compilation first
 *
 * @module cssmin
 */
gulp.task("cssmin", ["sass"]);

/**
 * Build documentation center
 *
 * @module doc
 */
gulp.task("doc", ["mainDoc", "componentDoc", "moduleDoc", "taskDoc", "styleguide"], function() {
  // notify once doc is done
  notify.notify({
    title: "Documentation",
    message: "Successfully built"
  });
});

/**
 * Build whole application.
 * Environment: "test", "development"
 */
gulp.task("build", ["cssmin", "svg", "html", "webpack"], function() {
  // notify once build is done
  notify.notify({
    title: "Build",
    message: "Successfully built"
  });
});

/**
 * Build whole application.
 * Environment: "production"
 */
gulp.task("prod", ["build", "prod:cssmin", "prod:svg", "prod:html", "prod:javascript"], function() {
  notify.notify({
    title: "Prod",
    message: "Successfully built production"
  });
});

/**
 * Serve doc and app
 */
gulp.task("server", ["doc", "serverDoc", "webpackDevServer"]);

/**
 * Watch build task
 */
gulp.task("watch", ["build"], function() {
  // once watch is done
  // watch again the files and build if changes
  gulp.watch([
    "./src/stylesheets/*.scss",
    "./src/stylesheets/**/*.scss",
    "./src/assets/images/*.svg",
    "./src/html/index.html"
  ], [
    "build"
  ]);
});
